//
//  PurchaseKJViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/9.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "PurchaseKJViewController.h"
#import "CommonData.h"
#import "UIAlertController+Evan.h"
#import "taskHomePageService.h"
#import "HomePassWordView.h"
#import <Masonry.h>
#import "MeModifyViewController.h"
#import "taskOverviewService.h"
#import "MeAuthenticationViewController.h"

@interface PurchaseKJViewController ()

@property (nonatomic,strong)    dataOpenBuyMinerInfo*   modelData;
@property (nonatomic,strong)    HomePassWordView*   viewPassword;
@property (nonatomic,assign)    BOOL isCheckGTE;
@property (weak, nonatomic) IBOutlet UILabel *lbUse;

@end

@implementation PurchaseKJViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.navigationController.navigationBar.hidden == YES) {
        self.navigationController.navigationBar.hidden = NO;
    }
    self.hidesBottomBarWhenPushed = YES;
    if (self.tabBarController.tabBar.hidden == NO) {
        self.tabBarController.tabBar.hidden = YES;
    }
    
    self.title = self.strTitle;
    
    self.viewBg.layer.cornerRadius = 10;
    self.viewBg.layer.shadowColor = [UIColor blackColor].CGColor;
    self.viewBg.layer.shadowOpacity = 0.3f;
    self.viewBg.layer.shadowRadius = 4.0f;
    
    [self loadData];
}

-(HomePassWordView*)viewPassword{
    if (!_viewPassword ){
        _viewPassword = [[[NSBundle mainBundle] loadNibNamed:@"HomePassWordView" owner:nil options:nil] objectAtIndex:0];
        [_viewPassword initUI];
        [self.view addSubview:_viewPassword];
        _viewPassword.hidden = YES;
        [_viewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.mas_equalTo(self.view);
            make.top.offset(60);
            make.bottom.mas_equalTo(self.view).offset(0);
        }];
    }
        return _viewPassword;
}

- (void)loadData{
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.sPid forKey:@"assetId"];
    [taskHomePageService getOpenBuyMiner:dict success:^(dataOpenBuyMiner *response, BOOL success) {
        if (success) {
            self.modelData = response.data;

//            if (self.partner == 0) {
//                self.labelSJ.text = [NSString stringWithFormat:@"%@元",self.modelData.AssetPriceDJ];
//                self.lbUse.text = [NSString stringWithFormat:@"%.2f个  china-A",[self.modelData.AssetPriceDJ floatValue] / [self.modelData.PriceRMB floatValue]];
//            }else{
//                self.labelSJ.text = [NSString stringWithFormat:@"%.2f元",[self.modelData.AssetPriceDJ floatValue] * [self.modelData.PriceRMB floatValue]];
//                self.lbUse.text = [NSString stringWithFormat:@"%@ china-A",self.modelData.AssetPriceDJ];
//            }
//            self.labelPriceCount.text = self.modelData.PriceRMB;

            if ([self.modelData.AssetType  isEqualToString: @"20030001"]) {
                self.labelSJ.text = [NSString stringWithFormat:@"%.2f元",([self.modelData.AssetPriceDJ floatValue] * self.modelData.PriceRMB)];
            }else{
                self.labelSJ.text = [NSString stringWithFormat:@"%@元",self.modelData.AssetPriceDJ];
            }
            
            self.labelChinaACount.text = [NSString stringWithFormat:@"%@个   china-A",self.modelData.AssetCoinNumber];
            self.labelPriceCount.text = [NSString stringWithFormat:@"%.2f元   china-A",self.modelData.PriceRMB];
        }else{
            [DKProgressHUD showErrorWithStatus:@"加载失败"];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (IBAction)btnNext:(id)sender {
    if (self.modelData.IsAddress == 0) {
        [DKProgressHUD showErrorWithStatus:@"请先添加收货地址"];
        return;
    }
    
    if ([self.txtTaiCount.text integerValue] < 0) {
        [DKProgressHUD showErrorWithStatus:@"请输入正确的购买数"];
        return;
    }
    
    if ([self.txtTaiCount.text integerValue] > [self.modelData.AssetCount integerValue]) {
        [[UIAlertController alloc]alertTitle:@"" alertMessage:@"库存不足" leftCancelTitle:@"确认" leftCancel:nil rightTitle:nil rightOK:nil ];
        return;
    }
    
//    if ([[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060004"] || [[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060003"]) {
//        [self.navigationController pushViewController:[MeAuthenticationViewController new] animated:YES];
//        return;
//    }else if ([[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060001"]){
//        [DKProgressHUD showErrorWithStatus:@"实名认证成功后才可以购买"];
//        return;
//    }
    
    if ([UserCenter shareCenter].user.WithdrawPwd.length < 1) {
        [self.navigationController pushViewController:[[MeModifyViewController alloc]initWithType:1] animated:YES];
        return;
    }
    
    if ([self.modelData.BuyMaxAtlasCount integerValue] < [self.txtTaiCount.text integerValue]) {
        [DKProgressHUD showErrorWithStatus:@"图册数量超出该资产包价格"];
        return;
    }
    
    ///进入输入密码的模块
    self.viewPassword.hidden = NO;
    __block PurchaseKJViewController *  blockSelf = self;
    self.viewPassword.blockSubmit = ^(NSString *pwd) {
        
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setValue:blockSelf.sPid forKey:@"assetId"];
        [dict setValue:@([blockSelf.txtTaiCount.text integerValue]) forKey:@"atlasNumber"];
        [dict setValue:pwd forKey:@"tradePwd"];
        [DKProgressHUD showLoading];
        [taskOverviewService getBuyAsset:dict success:^(BaseModel *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                blockSelf.viewPassword.hidden = YES;
                [DKProgressHUD showSuccessWithStatus:@"购买成功"];
                [blockSelf.navigationController popToRootViewControllerAnimated:NO];
                [blockSelf goRootBack:2];
            }else{
                [DKProgressHUD showErrorWithStatus:response.message];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            [DKProgressHUD dismiss];
            [DKProgressHUD showErrorWithStatus:strDescription];
        }];
    };
}


- (IBAction)btnCheckGTE:(id)sender {
    [self.btnCheckGTE setImage:[UIImage imageNamed:@"leftCheck"] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
